<template>
    <div>
        <el-tree
                ref="tree"
                :data="tableData"
                show-checkbox
                node-key="menuId"
                @check="checkNode"
                :props="defaultProps">
        </el-tree>
    </div>
</template>

<script>
    import {getMenu}from '@/api/loginAPI'
    import {insertRoleMenu,getRoleMenuId}from '@/api/menuApi'
    export default {
        props: {
            params: {
                type: Object,
                default () {
                    return {}
                }
            }
        },
        data(){
            return {
                tableData: [],
                defaultProps: {
                    children: 'children',
                    label: 'cnName'
                },
                form:{
                    roleId:'',
                    nodeCheck:[]
                },
                checked:[]
            }
        },
        methods:{
            checkNode(){
                const tempData=this.$refs.tree.getCheckedNodes(false,true)
                this.form.nodeCheck=tempData.map(it=>it.menuId)
                console.log( this.form.nodeCheck,999999)
            },
            submit(){
                return new Promise((resolve, reject) => {
                    insertRoleMenu(this.form).then(res => {
                        resolve(res.data)
                    }).catch(e => {
                        reject(e)
                        this.$message.error({
                            message: '抱歉，操作遇到异常，请重试或联系管理员处理'
                        })
                    })
                })
            }
        },
        mounted() {
            getRoleMenuId({roleId:this.params.roleId}).then(res=>{
                if(res.data.data.length>0){
                    this.form.nodeCheck=res.data.data.map(it=>it.menuId)
                    const tempData=(res.data.data.filter(it=>it.upperId!='0')).map(it=>it.menuId)
                    tempData.push('109')
                    this.$refs.tree.setCheckedKeys(tempData,true)

                }

            }).catch(e => {
                console.log(e,1111)
                this.$message.error({
                    message: '抱歉，读取菜单信息遇到异常，请重试或联系管理员处理'
                })
            })
            this.form.roleId=this.params.roleId
            getMenu({allFlag:'helloworld'}).then(res=>{
                this.tableData=res.data.data
                if(this.tableData.length>0){
                    this.tableData.forEach(it=>{
                        if(it.path=='/index'){
                            it.disabled=true
                        }
                    })
                }
            }).catch(e => {
                this.$message.error({
                    message: '抱歉，读取菜单信息遇到异常，请重试或联系管理员处理'
                })
            })
        }
    }
</script>

<style scoped>

</style>
